﻿namespace EFS.RC.Survey.Controls
{
	public partial class Answer : EFS.RC.Survey.Controls.Control
	{
		#region Constructors
		/// Implicit
		public Answer()
		{
			InitializeComponent();

			// do not use this constructor
			ConstructorNotAllowed();
		}

		/// Explicit
		public Answer(EFS.RC.CC.CH.Entities.Surveys.Answer answerObj)
		{
			// check
			EFS.Common.Verify.Reference(answerObj, "answerObj");

			// init
			InitializeComponent();

			// set
			this.AnswerObj = answerObj;

			// init data
			InitData();
		}

		public Answer(
			EFS.RC.CC.CH.Entities.Surveys.Answer answerObj, 
			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo sessionInfoObj, 
			EFS.RC.CC.CH.Entities.Surveys.Sessions.AnswerInfo[] answers)
		{
			// check
			EFS.Common.Verify.Reference(answerObj, "answerObj");

			// init
			InitializeComponent();

			// set
			this.AnswerObj = answerObj;
			this.SurveySessionObj = sessionInfoObj;
			this.AnswersInfoObj = answers;

			// init data
			InitData();
		}
		#endregion

		#region Properties
		/// Property AnswerObj: Get, Set
		public EFS.RC.CC.CH.Entities.Surveys.Answer AnswerObj { get; private set; }

		/// Property SurveySessionObj: Get, Set
		public EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo SurveySessionObj { get; private set; }

		/// Property AnswersObj: Get, Set
		public EFS.RC.CC.CH.Entities.Surveys.Sessions.AnswerInfo[] AnswersInfoObj { get; private set; }

		/// Property IsSelected: Get
		public System.Boolean IsSelected
		{
			get
			{
				return radioButton.IsChecked.HasValue && radioButton.IsChecked.Value;
			}
		}
		#endregion

		#region InitData
		/// InitData
		private void InitData()
		{
			// set the group name
			radioButton.GroupName = System.String.Format("myGroup_{0}", this.AnswerObj.QuestionID.Value);
		}
		#endregion

		#region Utilities
		/// Validate
		public System.Boolean Validate()
		{
			return true;
		}

		/// Show error
		private void ShowError(System.String err)
		{
			// prepare err
			System.String errTemp = EFS.Common.Functions.String.GetTrimmedString(err);
			textBlockValidationError.Text = errTemp;
			dockPanelValidation.Visibility = EFS.Common.Verify.String(textBlockValidationError.Text) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
		}
		#endregion

		#region Events
		/// Control loaded
		private void ControlAnswer_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// no error
			ShowError(null);

			// set answer
			textBlockAnswer.Text = AnswerObj.Text;

			if (SurveySessionObj != null)
			{
				foreach (EFS.RC.CC.CH.Entities.Surveys.Sessions.AnswerInfo answer in AnswersInfoObj)
				{
					if (AnswerObj.QuestionID == answer.QuestionId && AnswerObj.ID == answer.Answer.SurveyAnswerId)
					{
						radioButton.IsChecked = true;
					}
				}

				// check if read only
				if (SurveySessionObj.ReadOnly == true)
				{
					radioButton.IsEnabled = false;
				}
			}
		}
		#endregion
	}

}
		